package org.hao.samples.service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;
import org.hao.samples.dao.IRoleDAO;
import org.hao.samples.dao.IStudentDAO;
import org.hao.samples.model.PagerModel;
import org.hao.samples.model.Role;
import org.hao.samples.model.Student;
import org.hao.samples.model.StudentVO;
import org.hao.samples.util.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private IStudentDAO studentDAO;

	@Autowired
	private IRoleDAO roleDao;
	// @Autowired
	// private ExcelUtils ExcelUtils;

	@Override
	public List<Student> findAllStudent() {
		List<Student> st = studentDAO.findAllStudent();
		return st;
	}

	@Override
	public void createStudent(Student student) throws Exception {
		studentDAO.createStudent(student);
	}

	@Override
	public Student getUserById(Integer id) {
		return studentDAO.getUserById(id);
	}

	@Override
	public void editStudent(Student student) throws Exception {
		studentDAO.editStudent(student);
	}

	@Override
	public void deleteStudent(Integer id) throws Exception {
		studentDAO.deleteStudent(id);
		roleDao.deleteRole(id);
	}

	@Override
	public void saveStudentByExcel(InputStream is, String postfix) throws Exception {
		ExcelUtils excelUtils = new ExcelUtils();
		Workbook wk = ExcelUtils.openWorkbook(is, postfix);
		List<StudentVO> allExcelUser = excelUtils.getStudentListByExcel(wk);
		for (int i = 0; i < allExcelUser.size(); i++) {
			StudentVO vo = new StudentVO();
			BeanUtils.copyProperties(allExcelUser.get(i), vo);
			this.createStudent(vo);
		}
	}

	@Override
	public Role findRoleById(Integer user_id) {
		// TODO Auto-generated method stub
		return roleDao.findRoleById(user_id);
	}

	@Override
	public List<StudentVO> findAllStudentVO() {
		List<Student> student = studentDAO.findAllStudent();
		ArrayList<StudentVO> studentList = new ArrayList<StudentVO>();
		for (Student a : student) {
			StudentVO vo = new StudentVO();
			BeanUtils.copyProperties(a, vo);

			// date转String
			if (a.getBirthday() != null) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				java.util.Date date = a.getBirthday();
				String str = sdf.format(date);
				vo.setBirthday(str);
			}

			studentList.add(vo);
		}
		return studentList;
	}

	@Override
	public void createStudent(StudentVO studentvo) throws Exception {
		Student student = new Student();

		// string转date
		java.util.Date date = null;
		if (studentvo.getBirthday() != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
			String dstr = studentvo.getBirthday();
			date = sdf.parse(dstr);
		}
		BeanUtils.copyProperties(studentvo, student);
		student.setBirthday(date);
		student.setPassword("123456");
		student.setLast_lock_time(new Date());
		student.setLogin_fail_count(0);
		studentDAO.createStudent(student);

		// 加权限
		Role role = new Role();
		role.setRoleId(3);
		role.setUserId(studentvo.getUser_id());
		roleDao.createRole(role);
	}

	@Override
	public void editStudentVO(StudentVO vo) throws Exception {
		// string转date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
		String dstr = vo.getBirthday();
		java.util.Date date = sdf.parse(dstr);

		Student student = studentDAO.getUserById(vo.getUser_id());
		student.setUser_name(vo.getUser_name());
		student.setGender(vo.getGender());
		student.setBirthday(date);
		student.setTel(vo.getTel());
		student.setAddress(vo.getAddress());
		studentDAO.editStudent(student);
	}

	@Override
	public PagerModel<StudentVO> findAllByPage() {
		return studentDAO.findAllByPage();
	}
}
